﻿using AutoMapper;
using GGGK.Film.Comm;
using GGGK.Film.IService;
using GGGK.Film.Models;
using GGGK.Film.Models.Dto;
using GGGK.Film.Models.Models;
using Microsoft.AspNetCore.Mvc;

namespace GGGK.Film.Api.Controllers
{
    [Route("api/[controller]")]
    public class RolesController : ControllerBase
    {
        private readonly IRoleInfoService svc;
        private readonly IMapper mapper;

        public RolesController(IRoleInfoService svc, IMapper mapper)
        {
            this.svc = svc;
            this.mapper = mapper;
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="roleName"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public PagedResult<RoleInfoDto> Get(string? roleName, int pageIndex = 1, int pageSize = 10)
        {
            var query = svc.Query(o => true);

            if (!string.IsNullOrEmpty(roleName)) query = query.Where(o => o.RoleName.Contains(roleName));

            var total = query.Count();

            query = query.OrderByDescending(o => o.CreateTime).Skip(pageSize * (pageIndex - 1)).Take(pageSize);

            var dto = mapper.Map<IEnumerable<RoleInfoDto>>(query);

            return new PagedResult<RoleInfoDto>() { code = 1, msg = "成功", data = dto, total = total };

        }

        /// <summary>
        /// 获取全部
        /// </summary>
        /// <returns></returns>
        [HttpGet("GetAll")]
        public Result<IEnumerable<RoleInfoDto>> Get()
        {
            var data = svc.Query(o=>o.Status == 1);

            var dto = mapper.Map<IEnumerable<RoleInfoDto>>(data);

            return new Result<IEnumerable<RoleInfoDto>>() { code = 1, msg = "成功", data = dto };
        }

        /// <summary>
        /// 获取
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("{id}")]
        public Result<RoleInfoDto> Get(int id)
        {
            var data = svc.Get(o => o.Id == id);

            if (data == null) return new Result<RoleInfoDto>() { code = -1, msg = "数据不存在" };

            var dto = mapper.Map<RoleInfoDto>(data);

            return new Result<RoleInfoDto>() { code = 1, msg = "成功", data = dto };
        }

        /// <summary>
        /// 根据角色值获取
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        [HttpGet("getByValue/{value}")]
        public Result<RoleInfoDto> GetByValue(int value)
        {
            var data = svc.Get(o => o.RoleValue == value);

            if (data == null) return new Result<RoleInfoDto>() { code = -1, msg = "数据不存在" };

            var dto = mapper.Map<RoleInfoDto>(data);

            return new Result<RoleInfoDto>() { code = 1, msg = "成功", data = dto };
        }

        /// <summary>
        /// 角色添加
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public Result Post([FromBody]RolePostDto dto)
        {
            var entity = new Base_RoleInfo()
            {
                RoleName = dto.RoleName,
                RoleValue = dto.RoleValue,
                Remark = dto.Remark
            };

            entity.Status = 1;
            entity.CreateTime = DateTime.Now;

            if (svc.Add(entity))
                return new Result() { code = 1, msg = "成功" };
            else
                return new Result() { code = 1, msg = "失败" };

        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="id"></param>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPut]
        public Result Put(int id, [FromBody]RolePostDto dto)
        {
            var entity = svc.Get(o => o.Id == id);

            if (entity == null) return new Result() { code = -1, msg = "数据不存在" };

            entity.RoleName = dto.RoleName;
            entity.RoleValue = dto.RoleValue;
            entity.Remark = dto.Remark;

            if (svc.Update(entity))
                return new Result() { code = 1, msg = "成功" };
            else
                return new Result() { code = -1, msg = "失败" };
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public Result Delete(int id)
        {
            var entity = svc.Get(o => o.Id == id);

            if (entity == null) return new Result() { code = -1, msg = "数据不存在" };

            if (svc.Delete(entity))
                return new Result() { code = 1, msg = "成功" };
            else
                return new Result() { code = -1, msg = "失败" };
        }
    }
}
